package com.xiang.controller;

import io.jsonwebtoken.Jwts;
import java.nio.charset.StandardCharsets;
import javax.servlet.http.HttpServletRequest;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/user")
public class UserController {

  /**
   * 获取当前用户
   * @param authentication
   * @return
   */
  @RequestMapping("/getCurrentUser")
  public Object getCurrentUser(Authentication authentication, HttpServletRequest request) {
    String head = request.getHeader("Authorization");
    String token = head.substring("Bearer ".length());
//        return authentication.getPrincipal();
    return Jwts.parser().setSigningKey("test_key".getBytes(StandardCharsets.UTF_8))
        .parseClaimsJws(token)
        .getBody();
  }
}
